package com.example.migrator.batch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class BatchConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private MigrationItemReader migrationItemReader;

    @Autowired
    private MigrationItemProcessor migrationItemProcessor;

    @Autowired
    private MigrationItemWriter migrationItemWriter;

    /**
     * 创建数据迁移的作业
     */
    @Bean
    public Job migrationJob() {
        return jobBuilderFactory.get("migrationJob")
                .incrementer(new RunIdIncrementer())
                .flow(migrationStep())
                .end()
                .build();
    }

    /**
     * 创建数据迁移的步骤
     */
    @Bean
    public Step migrationStep() {
        return stepBuilderFactory.get("migrationStep")
                .<Object, Object>chunk(1000)
                .reader(migrationItemReader)
                .processor(migrationItemProcessor)
                .writer(migrationItemWriter)
                .build();
    }
}